#!/bin/bash

# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# Initialization
LOG=/dev/null
GROUP=students
GID=$(cat /etc/group | grep "$GROUP:" | cut -d: -f3)

if [[ -n $GID ]]
then
	grep $GID /etc/passwd | while read line
	do
		UNAME=$(echo "$line" | cut -d":" -f1)
		RNAME=$(echo "$line" | cut -d":" -f5)
		printf "Removing account %-40s" "'$RNAME' ($UNAME)"
		STATUS="FAILED"
		smbpasswd -x $UNAME 1> $LOG 2> $LOG
		[ $? -eq 0 ] && deluser --remove-home $UNAME 1> $LOG 2> $LOG
		[ $? -eq 0 ] && STATUS="OK"
		echo $STATUS
	done
	delgroup $GROUP
else
	echo "Group '$GROUP' does not contain members or does not exist." 1>&2
fi 
